﻿

#region Using directives
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pharmacy.Web.UI;
using Pharmacy.Settings.BusinessLayer;
using Pharmacy.Data;
using Pharmacy.Entities;
using System.Web.Services;
#endregion

public partial class Admin_Grn : System.Web.UI.Page
{	
    protected void Page_Load(object sender, EventArgs e)
	{
		FormUtil.RedirectAfterUpdate(GridView1, "Grn.aspx?page={0}");
		FormUtil.SetPageIndex(GridView1, "page");
		FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
        //if (!IsPostBack)
        //   WhereClauseHiddenField.Value=GetWhereClause();
    }
    protected string GetWhereClause()
    {
        string WhereClause = "left(GrnId,1)='G'"; //lay gia tri dau tien cua prefix
        return WhereClause;
    }
    
	protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
	{
		string urlParams = string.Format("GrnId={0}", GridView1.SelectedDataKey.Values[0]);
		Response.Redirect("GrnEdit.aspx?" + urlParams, true);
	}

    protected void GrnDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (String.IsNullOrEmpty(e.InputParameters["WhereClause"].ToString()))
            e.InputParameters["WhereClause"] = GetWhereClause();
        else
            e.InputParameters["WhereClause"] += " And " + GetWhereClause();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string grnID = e.CommandArgument.ToString().Trim();
        if (e.CommandName == "CustomDelete")
        {
            
            TList<Pharmacy.Entities.GrnDetails> objGrndetail = DataRepository.GrnDetailsProvider.GetByGrnId(grnID);
            if (objGrndetail.Count > 0)
            {
                Response.Write(@"<script language='javascript'>alert('Vui lòng xóa tất cả chi tiết.')</script>");

            }
            else
            {
                Pharmacy.Entities.Grn objGrn= new Pharmacy.Entities.Grn();
                objGrn.GrnId = grnID;
                DataRepository.GrnProvider.Delete(objGrn);
            }
              GridView1.DataBind();
        }
    }

    //[WebMethod]
    //public static void DeleteGrn(string GrnId)
    //{
    //    int count = 0;
    //    int countstock = 0;
    //    decimal Amount = 0;

    //    GrnDetails objGrnDetail = DataRepository.GrnDetailsProvider.GetByGrnId(GrnId);
    //    TList<PoDetails> listPoDetail = DataRepository.PoDetailsProvider.GetPaged("PoId='" + PoId + "' and DrugId ='" + objGrnDetail.DrugId + "'", "", 0, 1000, out  count);
    //    //set poquantity
    //    listPoDetail[0].RemainQuantity += objGrnDetail.Quantity;
    //    Amount = objGrnDetail.UnitPrice * objGrnDetail.Quantity;
    //    Grn objGrn = DataRepository.GrnProvider.GetByGrnId(objGrnDetail.GrnId);
    //    TransactionManager objTransactionManager = DataRepository.Provider.CreateTransaction();
    //    //update Podetails
    //    try
    //    {
    //        objTransactionManager.BeginTransaction();
    //        DataRepository.PoDetailsProvider.Update(objTransactionManager, listPoDetail[0]);
    //        DataRepository.GrnDetailsProvider.Delete(long.Parse(GrnDetailId));
    //        //update Amount Grn
    //        objGrn.Amount -= Amount;
    //        objGrn.TotalAmount = objGrn.Amount + objGrn.Vat;
    //        DataRepository.GrnProvider.Update(objGrn);

    //        objTransactionManager.Commit();
    //        GrnEdit updateDD = new GrnEdit();
    //        updateDD.GetWhereClause();


    //    }
    //    catch (Exception)
    //    {
    //        objTransactionManager.Rollback();

    //    }

    //}
}


